package com.wnhoo.smartfishtank.contants.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.wnhoo.smartfishtank.R;
import com.wnhoo.smartfishtank.api.data.DataTiming;
import com.wnhoo.smartfishtank.ui.UISwitchButton;

import java.util.List;


public class AdapterLightTiming extends BaseAdapter {
    // 填充数据的list
    private List<DataTiming> list;
    // 上下文
    private Context context;
    // 用来导入布局
    private LayoutInflater inflater = null;
    private final static String TAG = "AdapterLightTiming";

    // 构造器
    public AdapterLightTiming(List<DataTiming> list, Context context) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.light_timing_item, null);
        holder.light_timing_rel = convertView.findViewById(R.id.light_timing_rel);
        holder.light_timing_time_tv = (TextView) convertView.findViewById(R.id.light_timing_time_tv);
        holder.light_timing_color = (TextView) convertView.findViewById(R.id.light_timing_color);
        holder.light_timing_uibutton = (UISwitchButton) convertView.findViewById(R.id.light_timing_uibutton);
        convertView.setTag(holder);
        holder.light_timing_time_tv.setText(list.get(position).getTime_open() + "-" + list.get(position).getTime_colse());
        holder.light_timing_color.setVisibility(View.VISIBLE);
//            int color = getShowColor(list.get(position).getColor());
//        TAG -- AdapterLightTiming
        Log.i(TAG, "list.get(position).getColor()" + list.get(position).getColor());
        if (list.get(position).getColor() != null) {
            holder.light_timing_color.setBackgroundColor(Color.parseColor("#" + ImageAdapter.getColor()[Integer.parseInt(parseHexString(list.get(position).getColor()))]));
        }
        holder.light_timing_uibutton.setVisibility(View.VISIBLE);
        holder.light_timing_rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemTimingListener.ItemOnclick(position, v);
            }
        });
        return convertView;
    }

    private String parseHexString(String hexStr){
        if((hexStr.toLowerCase().indexOf("0a") != -1) || (hexStr.toLowerCase().indexOf("0b") != -1) ||
        (hexStr.toLowerCase().indexOf("0c") != -1) || (hexStr.toLowerCase().indexOf("0d") != -1) ||
                (hexStr.toLowerCase().indexOf("0e") != -1) || (hexStr.toLowerCase().indexOf("0f") != -1)){
            return Integer.valueOf(hexStr, 16).toString();
        }else {
            return hexStr;
        }
    }

    private int getShowColor(String hexcolor) {
        switch (hexcolor) {
            case "33cc33":
                return 0xff33cc33;
            case "66cc66":
                return 0xff66cc66;
            case "cccc66":
                return 0xffcccc66;
            case "4c4ccc":
                return 0xff4c4ccc;
            case "4ccccc":
                return 0xff4ccccc;
            case "7fcc7f":
                return 0xff7fcc7f;
            default:
                return 0xff33cc33;
        }
    }

    final class ViewHolder {
        TextView light_timing_time_tv, light_timing_color;
        UISwitchButton light_timing_uibutton;
        View light_timing_rel;
    }

    private OnItemTimingListener onItemTimingListener;

    public void setOnItemTimingListener(OnItemTimingListener l) {
        onItemTimingListener = l;
    }

    public interface OnItemTimingListener {
        boolean ItemOnclick(int position, View view);

        void onUISwitchChange(int position, CompoundButton compoundButton, boolean b);
    }
}
